package com.todaynews.ui.smallvideo;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.viewpager.widget.ViewPager;
import com.todaynews.R;
import com.todaynews.frame.model.InfoResult;
import com.todaynews.frame.ui.barlibrary.ImmersionBar;
import com.todaynews.frame.ui.fragment.BaseFragment;
import com.todaynews.frame.ui.views.colortrackview.ColorClipTabLayout;
import com.todaynews.model.model.Channel;
import com.todaynews.ui.home.NewsListFragment;
import com.todaynews.ui.home.adapter.ChannelPagerAdapter;
import com.todaynews.ui.smallvideo.adapter.ChannelSmallVideoPagerAdapter;
import com.todaynews.utils.Constants;
import com.todaynews.utils.screen.DensityUtil;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import cn.jzvd.Jzvd;

public class SmallVideoFragment extends BaseFragment {


    @BindView(R.id.tab_channel)
    ColorClipTabLayout tabChannel;
    @BindView(R.id.linear_add)
    LinearLayout linearAdd;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private List<Channel> channels = new ArrayList<>();
    private List<SmallVideoListFragment> channelFragments = new ArrayList<>();
    private ChannelSmallVideoPagerAdapter channelSmallVideoPagerAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.white)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_small_video;
    }

    @Override
    public void init() {
        initChannelData();
        initChannelFragments();
        initData();
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    /**
     * 初始化频道数据
     */
    private void initChannelData() {
        String[] datas = getResources().getStringArray(R.array.smallvideo);
        for (int i = 0; i < datas.length; i++) {
            String title = datas[i];
            channels.add(new Channel(title));
        }
    }

    /**
     * 初始化已选频道的fragment的集合
     */
    private void initChannelFragments() {
        for (Channel channel : channels) {
            SmallVideoListFragment newsFragment = new SmallVideoListFragment();
            channelFragments.add(newsFragment);//添加到集合中
        }
    }

    private void initData() {

        channelSmallVideoPagerAdapter = new ChannelSmallVideoPagerAdapter(channelFragments, channels,
                getChildFragmentManager());
        vpContent.setAdapter(channelSmallVideoPagerAdapter);
        vpContent.setOffscreenPageLimit(channels.size());
        tabChannel.setTabPaddingLeftAndRight(DensityUtil.getInstance().dpToPx(5), DensityUtil.getInstance().dpToPx(5));
        tabChannel.setupWithViewPager(vpContent);
        tabChannel.post(new Runnable() {
            @Override
            public void run() {
                //设置最小宽度，使其可以在滑动一部分距离
                ViewGroup slidingTabStrip = (ViewGroup) tabChannel.getChildAt(0);
                slidingTabStrip.setMinimumWidth(slidingTabStrip.getMeasuredWidth());
            }
        });
        //隐藏指示器
        tabChannel.setSelectedTabIndicatorHeight(0);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
